﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management;
using System.Text;
using WMI.NET.Abstracts;
using WMI.NET.CIM;

namespace WMI.NET.ComputerSystemHardware.Printing
{
    /// <summary>
    /// The Win32_PrinterDriver WMI class represents the drivers for a Win32_Printer instance. 
    /// </summary>
    public class Win32_PrinterDriver : CIM_Service
    {

        private string _ConfigFile;
        private string _DataFile;
        private string _DefaultDataType;
        private string[] _DependentFiles;
        private string _DriverPath;
        private string _FilePath;
        private string _HelpFile;
        private string _InfName;
        private string _MonitorName;
        private string _OEMUrl;
        private string _SupportedPlatform;
        private Nullable<UInt16> _Version;

        /// <summary>
        /// Configuration file for this printer driver.
        /// Example: "pscrptui.dll"
        /// </summary>
        public string ConfigFile { get { return _ConfigFile; } }

        /// <summary>
        /// Data file for this printer driver.
        /// Example: "qms810.ppd"
        /// </summary>
        public string DataFile { get { return _DataFile; } }

        /// <summary>
        /// Default data type for this printer driver.
        /// Example: "EMF"
        /// </summary>
        public string DefaultDataType { get { return _DefaultDataType; } }

        /// <summary>
        /// Array of dependent files for this printer driver.
        /// </summary>
        public string[] DependentFiles { get { return _DependentFiles; } }

        /// <summary>
        /// Path for this printer driver.
        /// Example: "C:\\drivers\\pscript.dll"
        /// </summary>
        public string DriverPath { get { return _DriverPath; } }

        /// <summary>
        /// Path to the INF file being used.
        /// Example: "c:\\temp\\driver"
        /// </summary>
        public string FilePath { get { return _FilePath; } }

        /// <summary>
        /// Help file for this printer driver.
        /// Example: "pscrptui.hlp"
        /// </summary>
        public string HelpFile { get { return _HelpFile; } }

        /// <summary>
        /// Name of the INF file being used. The default is to use an operating system provided printer INF file. A different file name is used if the driver is provided directly by the manufacturer of the printer and not the operating system.
        /// Windows Server 2003 and Windows XP:  The default name is "ntprint.inf".
        /// </summary>
        public string InfName { get { return _InfName; } }

        /// <summary>
        /// Name of the monitor for this printer driver.
        /// Example: "PJL monitor"
        /// </summary>
        public string MonitorName { get { return _MonitorName; } }

        /// <summary>
        /// World Wide Web (WWW) link to the printer manufacturer's website. Note that this property is not populated when the Win32.inf file is used, and is only applicable for drivers provided directly from the manufacturer.
        /// </summary>
        public string OEMUrl { get { return _OEMUrl; } }

        /// <summary>
        /// Operating environments that the driver is intended for.
        /// Example: "Windows NT x86".
        /// </summary>
        public string SupportedPlatform { get { return _SupportedPlatform; } set { throw new NotImplementedException(); } }

        /// <summary>
        /// Operating system version for the printer driver.
        /// </summary>
        public Nullable<UInt16> Version { get { return _Version; } set { throw new NotImplementedException(); } }


        /// <summary>
        /// The AddPrinterDriver class method creates a new printer driver.
        /// Note  When using the AddPrinterDriver method you must use SeLoadDriverPrivilege to load or unload a device driver.
        /// </summary>
        /// <param name="DriverInfo">Information needed to create the printer driver.</param>
        /// <returns></returns>
        UInt32 AddPrinterDriver(Win32_PrinterDriver DriverInfo) { throw new NotImplementedException(); }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        public Win32_PrinterDriver(ManagementBaseObject mbo)
            : base(mbo)
        {
            _ConfigFile = mbo.GetStringValue("ConfigFile");
            _DataFile = mbo.GetStringValue("DataFile");
            _DefaultDataType = mbo.GetStringValue("DefaultDataType");
            _DependentFiles = mbo.GetStringValueArray("DependentFiles");
            _DriverPath = mbo.GetStringValue("DriverPath");
            _FilePath = mbo.GetStringValue("FilePath");
            _HelpFile = mbo.GetStringValue("HelpFile");
            _InfName = mbo.GetStringValue("InfName");
            _MonitorName = mbo.GetStringValue("MonitorName");
            _OEMUrl = mbo.GetStringValue("OEMUrl");
            _SupportedPlatform = mbo.GetStringValue("SupportedPlatform");
            _Version = mbo.GetUshortValue("Version");
        }

    }

    /// <summary>
    /// 
    /// </summary>
    public class GetWin32_PrinterDrivers : Win32Collection<Win32_PrinterDriver>
    {

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ReadOnlyCollection<Win32_PrinterDriver> GetCollection()
        {
            List<Win32_PrinterDriver> tempCollection = new List<Win32_PrinterDriver>();

            foreach (var s in searcher.Get())
            {
                Win32_PrinterDriver tempListItem = new Win32_PrinterDriver(s);
                tempCollection.Add(tempListItem);
            }

            return tempCollection.AsReadOnly();
        }

        /// <summary>
        /// 
        /// </summary>
        public GetWin32_PrinterDrivers() : base("SELECT * FROM Win32_PrinterDriver") { }

    }

}
